package com.sudo.boot.netty.packet;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;

public class NettyMessageClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {
    // 连接通道创建后需要向服务端发送消息


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 50; i++) {
            byte[] msg = "客户端起来了呀".getBytes(StandardCharsets.UTF_8);
            MessageProtocol mp = new MessageProtocol();
            mp.setLength(msg.length);
            mp.setContent(msg);
            // ctx.writeAndFlush 只能发送ByteBuf数据，需要用编码器把它编码成ByteBuf数据
            ctx.writeAndFlush(mp);
        }
    }

    /**
     *
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol mp) throws Exception {

    }
}
